import { Injectable } from '@nestjs/common'
import { CreateCommontDto } from './dto/create-commont.dto'
import { PrismaService } from 'src/common/prisma/prisma.service'

@Injectable()
export class CommontService {
   constructor(private prisma: PrismaService) {}
   async create(createCommontDto: CreateCommontDto, aid: number, userId: number) {
      return await this.prisma.commont.create({
         data: {
            content: createCommontDto.content,
            articel: { connect: { id: +aid } },
            user: { connect: { id: userId } },
            reply: createCommontDto?.commontId && { connect: { id: +createCommontDto.commontId } },
            replyUser: createCommontDto?.replyUserId && { connect: { id: +createCommontDto.replyUserId } }
         },
         include: {
            user: true,
            replys: true,
            replyUser: true
         }
      })
   }

   async findOne(id: number) {
      return await this.prisma.commont.findFirst({
         where: { id },
         include: {
            user: true,
            replys: {
               include: {
                  user: true
               }
            }
         }
      })
   }

   async findAll(id: number) {
      const res = await this.prisma.commont.findMany({
         where: {
            articelId: +id,
            AND: {
               commontId: null
            }
         },
         include: {
            user: true,
            replys: {
               include: {
                  user: true,
                  replyUser: true
               }
            }
         }
      })

      return res
   }

   async removeCommont(id: number) {
      return await this.prisma.commont.delete({
         where: { id }
      })
   }
}
